import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutterapp2/bugly/capture_widget.dart';

void launch(Widget mainApp) {
  bool isReleaseMode = true;
  assert(() {
    isReleaseMode = false;
    return true;
  }());

  ErrorWidget.builder = (details) {
    if (isReleaseMode) {
//      _reportErrorDetails(details);
    }
    return CaptureErrorWidget(
      error: details.exceptionAsString(),
      stack: details.stack.toString(),
    );
  };

  FlutterError.onError = (details) {
    if (isReleaseMode) {
//      _reportErrorDetails(details);
    } else {
      FlutterError.dumpErrorToConsole(details);
    }
  };

  runZoned<Future<void>>(() async {
    runApp(mainApp);
  }, onError: (error, stackTrace) {
    _reportError(error, stackTrace);
  });
}

void _reportError(dynamic exception, StackTrace stack) {
  FlutterError.reportError(FlutterErrorDetails(
    exception: exception,
    stack: stack,
    library: 'foundation library',
    context: ErrorDescription('while dispatching notifications for '),
  ));
}
